<template>
  <div>
    <div ref="toolbar" class="toolbar"></div>
    <div ref="editor" class="content"></div>
  </div>
</template>

<script>
  const E = window.wangEditor
export default {
  name: "NoteDisplay",
  data() {
    return {
      editor: null,
      info_: null
    }
  },
  mounted() {
    let that = this
    that.setEditor()
    that.editor.txt.html(that.value)
  },
  model: {
    prop: 'value',
    event: 'change'
  },
  props: {
    value: {
      type: String,
      default: null
    },
    isClear: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    isClear(val) {
      // 触发清除文本域内容
      if (val) {
        this.editor.txt.clear()
        this.info_ = null
      }
    },
    value: function(value) {
      if (value !== this.editor.txt.html()) {
        this.editor.txt.html(this.value)
      }
    }
  },
  methods: {
    setEditor() {
      this.editor = new E(this.$refs.toolbar, this.$refs.editor)
      this.editor.customConfig = this.editor.customConfig ? this.editor.customConfig : this.editor.config
      this.editor.config.focus = false

      // 配置菜单
      this.editor.customConfig.menus = [
        'fontSize', // 字号
        'bold', // 粗体
        'italic', // 斜体
        'underline', // 下划线
        'strikeThrough', // 删除线
        'justify', // 对齐方式
      ]
      this.editor.customConfig.onchange = (html) => {
        this.info_ = html // 绑定当前逐渐地值
        this.$emit('change', this.info_) // 将内容同步到父组件中
      }
      this.editor.create()
    }
  }
}
</script>

<style lang="less">
.toolbar {
  border-bottom: 1px solid #ccc;
}
.content {
  height: 72vh;
  overflow-y: hidden;
}
.w-e-menu {
  z-index: 3 !important;
}
.w-e-toolbar {
  z-index: 2 !important;
  background-color: #FFFAF4;
}
.w-e-text-container {
  z-index: 1 !important;
  background-color: #FFFAF4;
}
.w-e-menu-tooltip-up {
  visibility: hidden!important;
}
</style>
